﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.BL;

namespace ADC.Web.Admin
{
    public partial class ListFileUpload : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListFileUpload");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InintData();
            }
        }

        private void InintData()
        {
            try
            {
                var files = FileUploadFactory.GetAll();
                foreach (var file in files)
                {
                    file.Link = Request.Url.Scheme + "://" + Request.Url.DnsSafeHost + ":" + Request.Url.Port + "/" + file.FilePath;
                    file.FilePath = "../" + file.FilePath;

                }
                GridViewSlide.DataSource = files;
                GridViewSlide.DataBind();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }

        }


        protected void GridViewSlide_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblSlideID = (Label)GridViewSlide.Rows[e.NewEditIndex].FindControl("lblSlideID");
                if (lblSlideID != null && !string.IsNullOrEmpty(lblSlideID.Text))
                {
                    Response.Redirect("ManageFileUpload.aspx?Mode=1&ID=" + lblSlideID.Text, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewSlide_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblSlideID = (Label)GridViewSlide.Rows[e.RowIndex].FindControl("lblSlideID");
                var lblImagePath = (Label)GridViewSlide.Rows[e.RowIndex].FindControl("lblImagePath");
                if (lblSlideID != null && !string.IsNullOrEmpty(lblSlideID.Text))
                {
                    // Delete in database
                    FileUploadFactory.Delete(Convert.ToInt32(lblSlideID.Text));
                    InintData();

                    // Delete old image
                    var filePath = Request.PhysicalApplicationPath + lblImagePath.Text.Substring(3, lblImagePath.Text.Length - 3);
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageFileUpload.aspx?Mode=0");
        }
    }
}